package com.health.fragment.jtys;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.health.tablebean.CD_DIABETESFOLLOWUP;
import com.health.tablebean.CD_HYPERTENSIONFOLLOWUP;
import com.health.tools.GlobalContsJTYS;
import com.health.view.spinner.CircularProgressView;
import com.health.viewscontrols.AddCustomer;
import com.health.viewscontrols.LoginActivity;
import com.health.viewscontrols.MYDActivity;
import com.health.viewscontrols.MainActivity_Menu_JTYS;
import com.health.viewscontrols.NovaApplication;
import com.health.R;
import com.health.db.DbManager;
import com.health.fragment.NovaFragment;
import com.health.model.PeopleType;
import com.health.tablebean.JTYSDoctorEvaluateBean;
import com.health.tools.SPUtil;

import android.animation.ObjectAnimator;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import net.tsz.afinal.db.sqlite.DbModel;

/**
 * 医生管理员首页
 * 
 * @author nova
 *
 */
public class JTYSHome extends NovaFragment implements OnClickListener {

	private View docHomeView = null;
	public static final int ANIMATOR_DURATION = 1000;
	private CircularProgressView progressView;
	// private TextView tv_jgglrs;// 机构管理人数
	private TextView tv_jrxz;// 今日新增
	// private TextView tv_jbrq;// 疾病人群
	private TextView tv_jrjc;// 今日监测
	// private TextView tv_gxy;// 高血压
	private TextView tv_jrpg;// 今日评估
	// private TextView tv_tnb;// 糖尿病
	private TextView tv_jrwz;// 今日问诊
	// private TextView tv_ycf;// 孕产妇
	private TextView tv_qyrs;// 签约人数
	private TextView tv_wqy;// 未签约
	private DbManager dbManager = null;
	private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
	private TextView tv_myl;
	private TextView tv_fcmy;
	private TextView tv_my;
	private TextView tv_ybmy;
	private TextView tv_bmy;
	private TextView tv_fcbmy;
	private ListView lv_tixing;
	private LinearLayout ll_jrxz;
	private LinearLayout ll_jrjc;
	private LinearLayout ll_qyrs;
	private LinearLayout ll_wqyrs;
	private RelativeLayout ll_fcmy;
	private RelativeLayout ll_my;
	private RelativeLayout ll_ybmy;
	private RelativeLayout ll_bmy;
	private RelativeLayout ll_fcbmy;
	// private Button btn_add;
	public static String myd = "3";
	/**
	 * 重点人群相关
	 **/
	private TextView tv_ybrq;// 一般人群
	private TextView tv_gxy;// 高血压
	private TextView tv_ertong;// 儿童
	private TextView tv_tnb;// 糖尿病
	private TextView tv_ycf;// 孕产妇
	private TextView tv_gxb;// 冠心病
	private TextView tv_lnr;// 老年人
	private TextView tv_nzz;// 脑卒中
	private TextView tv_zxjsjb;// 重型精神疾病
	private TextView tv_cjr;// 残疾人
	private TextView tv_fjh;// 肺结核


    TextView tv_jiandang_number;//建档人数
    TextView tv_jiandang_yes;//建档已上传
     TextView tv_jiandang_no;//建档未上传
    TextView tv_qianyue_number;//签约人数
    TextView tv_qianyue_yes;//签约已上传
    TextView  tv_qianyue_no;//签约未上传
    TextView tv_suifang_number;//随访总数
    TextView tv_suifang_yes;//随访已上传
    TextView tv_suifang_no;//随访未上传
    TextView tv_tijian_number;//体检总数
    TextView tv_tijian_yes;//体检已上传
    TextView tv_tijian_no;//体检未上传
	TextView welcome_doctor;
	private TextView tvJg;
	private TextView tv_nowjd;
	private TextView tv_nowqy;

	@Override
	public View findViews(LayoutInflater inflater) {
		dbManager = ((NovaApplication) getActivity().getApplication()).getDbManger();

		docHomeView = inflater.inflate(R.layout.jtys_home, null);
		initViews(docHomeView);
		return docHomeView;
	}

	@Override
	public void setupView(View view) {
		// TODO Auto-generated method stub

	}

	@Override
	public void addListener() {

		// btn_add.setOnClickListener(this);
		ll_jrxz.setOnClickListener(this);
		ll_jrjc.setOnClickListener(this);
		ll_qyrs.setOnClickListener(this);
		ll_wqyrs.setOnClickListener(this);
		ll_fcmy.setOnClickListener(this);
		ll_my.setOnClickListener(this);
		ll_ybmy.setOnClickListener(this);
		ll_bmy.setOnClickListener(this);
		ll_fcbmy.setOnClickListener(this);
	}

	@Override
	public void setData() {
		View head = View.inflate(getActivity(), R.layout.jkjy_home_list, null);
		lv_tixing.addHeaderView(head);
	}

	/**
	 * 初始化布局
	 * 
	 * @author 王婷婷
	 * @date 2017年2月10日 下午3:17:44
	 */
	private void initViews(View v) {
		// tv_jgglrs = (TextView) v.findViewById(R.id.tv_jgglrs);
		tv_jrxz = (TextView) v.findViewById(R.id.tv_jrxz);// 今日新增
		// tv_jbrq = (TextView) v.findViewById(R.id.tv_jbrq);// 疾病人群
		tv_nowqy = (TextView)v.findViewById(R.id.tv_nowqy);
		tv_nowjd = (TextView)v.findViewById(R.id.tv_nowjd);
		tv_jrjc = (TextView) v.findViewById(R.id.tv_jrjc);// 今日监测
		// tv_gxy = (TextView) v.findViewById(R.id.tv_gxy);// 高血压
		tv_jrpg = (TextView) v.findViewById(R.id.tv_jrpg);// 今日评估
		// tv_tnb = (TextView) v.findViewById(R.id.tv_tnb);// 糖尿病
		tv_jrwz = (TextView) v.findViewById(R.id.tv_jrwz);// 今日问诊
		// tv_ycf = (TextView) v.findViewById(R.id.tv_ycf);// 孕产妇
		tv_qyrs = (TextView) v.findViewById(R.id.tv_qyrs);
		tv_wqy = (TextView) v.findViewById(R.id.tv_wqy);// 未签约
		LinearLayout ll_back = (LinearLayout)v.findViewById(R.id.ll_back);
		ll_back.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View view) {
				getActivity().finish();
			}
		});

		// btn_add = (Button) v.findViewById(R.id.btn_add);

		progressView = (CircularProgressView)v.findViewById(R.id.circle_percent_progress);

		tv_myl = (TextView) v.findViewById(R.id.tv_myl);// 满意率
		tv_fcmy = (TextView) v.findViewById(R.id.tv_fcmy);// 非常满意
		tv_my = (TextView) v.findViewById(R.id.tv_my);// 满意
		tv_ybmy = (TextView) v.findViewById(R.id.tv_ybmy);// 一般满意
		tv_bmy = (TextView) v.findViewById(R.id.tv_bmy);// 不满意
		tv_fcbmy = (TextView) v.findViewById(R.id.tv_fcbmy);// 非常不满意
		lv_tixing = (ListView) v.findViewById(R.id.lv_tixing);// 非常不满意
		ll_jrxz = (LinearLayout) v.findViewById(R.id.ll_jrxz);
		ll_jrjc = (LinearLayout) v.findViewById(R.id.ll_jrjc);
		ll_qyrs = (LinearLayout) v.findViewById(R.id.ll_qyrs);
		ll_wqyrs = (LinearLayout) v.findViewById(R.id.ll_wqyrs);
		ll_fcmy = (RelativeLayout) v.findViewById(R.id.ll_fcmy);
		ll_my = (RelativeLayout) v.findViewById(R.id.ll_my);
		ll_ybmy = (RelativeLayout) v.findViewById(R.id.ll_ybmy);
		ll_bmy = (RelativeLayout) v.findViewById(R.id.ll_bmy);
		ll_fcbmy = (RelativeLayout) v.findViewById(R.id.ll_fcbmy);

//		LinearLayout ll_back = (LinearLayout)v.findViewById(R.id.ll_back);
//		ll_back.setOnClickListener(this);

		/* 重点人群相关 */
		tv_ybrq = (TextView) v.findViewById(R.id.tv_ybrq);
		tv_gxy = (TextView) v.findViewById(R.id.tv_gxy);
		tv_ertong = (TextView) v.findViewById(R.id.tv_ertong);
		tv_tnb = (TextView) v.findViewById(R.id.tv_tnb);
		tv_ycf = (TextView) v.findViewById(R.id.tv_ycf);
		tv_gxb = (TextView) v.findViewById(R.id.tv_gxb);
		tv_lnr = (TextView) v.findViewById(R.id.tv_lnr);
		tv_nzz = (TextView) v.findViewById(R.id.tv_nzz);
		tv_zxjsjb = (TextView) v.findViewById(R.id.tv_zxjsjb);
		tv_cjr = (TextView) v.findViewById(R.id.tv_cjr);
		tv_fjh = (TextView) v.findViewById(R.id.tv_fjh);

		welcome_doctor=v.findViewById(R.id.welcome_doctor);

        tv_jiandang_number=v.findViewById(R.id.tv_jiandang_number);
        tv_jiandang_yes=v.findViewById(R.id.tv_jiandang_yes);
        tv_jiandang_no=v.findViewById(R.id.tv_jiandang_no);
        tv_qianyue_number=v.findViewById(R.id.tv_qianyue_number);
        tv_qianyue_yes=v.findViewById(R.id.tv_qianyue_yes);
        tv_qianyue_no=v.findViewById(R.id.tv_qianyue_no);
        tv_suifang_number=v.findViewById(R.id.tv_suifang_number);
        tv_suifang_yes=v.findViewById(R.id.tv_suifang_yes);
        tv_suifang_no=v.findViewById(R.id.tv_suifang_no);
        tv_tijian_number=v.findViewById(R.id.tv_tijian_number);
        tv_tijian_yes=v.findViewById(R.id.tv_tijian_yes);
        tv_tijian_no=v.findViewById(R.id.tv_tijian_no);
		tvJg = (TextView)v.findViewById(R.id.tv_jg);
		SharedPreferences shareDatas = getActivity().getSharedPreferences(SPUtil.SP_NAME, Context.MODE_PRIVATE);
		String jg = shareDatas.getString(SPUtil.STR_JDDW,"");
		tvJg.setText(jg);
	}

	/**
	 * 设置百分比
	 * @param max     最大值
	 * @param current 占比
	 */
	private void setProgressViewData(int max, float current) {

		float percentage = (100f * current) / max;
		ObjectAnimator animator = ObjectAnimator.ofFloat(progressView, "percentage", 0, percentage);
		animator.setDuration(ANIMATOR_DURATION);
		animator.start();
	}

	private MainActivity_Menu_JTYS parentActivity;

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_add:
			Intent intent = new Intent(getActivity(), AddCustomer.class);
			startActivity(intent);
			break;
		// case R.id.ll_jrxz:
		// case R.id.ll_jrjc:
		// case R.id.ll_qyrs:
		// case R.id.ll_wqyrs:
		// // 跳转到档案界面
		// parentActivity = (MainActivity_Menu_JTYS) getActivity();
		// parentActivity.viewPager.setCurrentItem(2, false);
		// break;
		case R.id.ll_fcmy:
			myd = "1";
			goMYD();
//			parentActivity.viewPager.setCurrentItem(3, false);
			break;
		case R.id.ll_my:
			myd = "0";
			goMYD();
//			parentActivity.viewPager.setCurrentItem(3, false);
			break;
		case R.id.ll_ybmy:
			myd = "4";
			goMYD();
//			parentActivity.viewPager.setCurrentItem(3, false);
			break;
		case R.id.ll_bmy:
			myd = "2";
			goMYD();
//			parentActivity.viewPager.setCurrentItem(3, false);
			break;
		case R.id.ll_fcbmy:
			// 跳转到满意度界面
			myd = "5";
			goMYD();
//			parentActivity.viewPager.setCurrentItem(3, false);
			break;
			case R.id.ll_back:
				getActivity().finish();
				break;
		default:
			break;
		}

	}

	private void goMYD(){
		Intent intent = new Intent(getActivity(), MYDActivity.class);
		startActivity(intent);
	}

	@Override
	public void onResume() {
		super.onResume();
		String str_currentDate = dateFormat.format(new Date());
		String str_jdr = SPUtil.getstr_jdr(getActivity());
		String str_jdrno = SPUtil.getStr_jdrno(getActivity());

		welcome_doctor.setText(str_jdr);
		// tv_jgglrs.setText(String.valueOf(dbManager.getCountByJDR(str_jdr,
		// str_jdrno)));
//		tv_jrxz.setText(String.valueOf(dbManager.getCountByJDRCurrentDate(str_jdrno, str_currentDate)));
		tv_nowjd.setText(String.valueOf(dbManager.getCountByJDRCurrentDate(str_jdrno, str_currentDate)));
		// 疾病人群
		// tv_jbrq.setText(String.valueOf(dbManager.getCountByJDRJBType(str_jdrno)));
		// 今日监测
		tv_jrjc.setText(String.valueOf(dbManager.getCountByJDRCurrentDate(str_jdrno, str_currentDate)));
		// 高血压
		// tv_gxy.setText(String.valueOf(dbManager.getCountByJDRType(str_jdrno,
		// "6")));
		// 糖尿病
		// tv_tnb.setText(String.valueOf(dbManager.getCountByJDRType(str_jdrno,
		// "7")));
		// 孕产妇
		// tv_ycf.setText(String.valueOf(dbManager.getCountByJDRType(str_jdrno,
		// "3")));
//		tv_wqy.setText(String.valueOf(dbManager.getCountByJDRUnSign(str_jdrno, str_jdrno,"")));
		tv_jiandang_number.setText("/"+String.valueOf(dbManager.getCountByJDR(str_jdrno)));
		tv_jiandang_yes.setText(String.valueOf(dbManager.getCountByJDRUp(str_jdrno)));
        tv_jiandang_no.setText(String.valueOf(dbManager.getCountByJDRNoUp(str_jdrno)));

//		tv_nowqy.setText(String.valueOf(dbManager.getCountByJDRNowSign(getActivity(),str_jdrno,GlobalContsJTYS.dateFormat.format(new Date()))));
		List<CD_DIABETESFOLLOWUP> tnbList = NovaApplication.fdbmanger
				.findAllByWhere(CD_DIABETESFOLLOWUP.class, "CREATER = '" + str_jdrno +"'");
		List<CD_HYPERTENSIONFOLLOWUP> gxyList = NovaApplication.fdbmanger
				.findAllByWhere(CD_HYPERTENSIONFOLLOWUP.class, "CREATER = '" + str_jdrno +"'");
;		Log.e("xiaoxin", "onResume: " + tnbList.size() + "  " + gxyList.size());
		int size = tnbList.size() + gxyList.size();
		tv_nowqy.setText("" + size);

		try {
			DbModel tnbYModel = NovaApplication.fdbmanger.findDbSql(
					"SELECT COUNT(ID) AS number FROM CD_DIABETESFOLLOWUP WHERE IS_UPDATE = 'Y' AND CREATER = '" + str_jdrno +"'");
			DbModel tnbNModel = NovaApplication.fdbmanger.findDbSql(
					"SELECT COUNT(ID) AS number FROM CD_DIABETESFOLLOWUP WHERE IS_UPDATE = 'N' AND CREATER = '" + str_jdrno +"'");
			DbModel gxyYModel = NovaApplication.fdbmanger.findDbSql(
					"SELECT COUNT(ID) AS number FROM CD_HYPERTENSIONFOLLOWUP WHERE IS_UPDATE = 'Y' AND CREATER = '" + str_jdrno +"'");
			DbModel gxyNModel = NovaApplication.fdbmanger.findDbSql(
					"SELECT COUNT(ID) AS number FROM CD_HYPERTENSIONFOLLOWUP WHERE IS_UPDATE = 'N' AND CREATER = '" + str_jdrno +"'");

			int tnbYCount = tnbYModel.getInt("number");
			int tnbNCount = tnbNModel.getInt("number");
			int gxyYCount = gxyYModel.getInt("number");
			int txyNCount = gxyNModel.getInt("number");
			Log.e("xiaoxin", "onResume: "+tnbNCount+txyNCount );
			Log.e("xiaoxin", "onResume: "+tnbYCount+gxyYCount );
			tv_qianyue_yes.setText(String.valueOf(tnbNCount+txyNCount));
			tv_qianyue_no.setText(String.valueOf(tnbYCount+gxyYCount));
		} catch (Exception e) {
			e.printStackTrace();
		}

//		tv_qianyue_number.setText("/"+String.valueOf(dbManager.getCountByJDRSign(getActivity(),str_jdrno)));
//        tv_qianyue_yes.setText(String.valueOf(dbManager.getCountByJDRSignYes(str_jdrno)));
//		tv_qianyue_no.setText(String.valueOf(dbManager.getCountByJDRSignNo(str_jdrno)));
		tv_my.setText(
				NovaApplication.fdbmanger
						.findAllByWhere(JTYSDoctorEvaluateBean.class,
								"satisfiedState='" + 0 + "'" + " and creater='" + NovaApplication.getJgrno() + "'")
						.size() + "");
		tv_fcmy.setText(
				NovaApplication.fdbmanger
						.findAllByWhere(JTYSDoctorEvaluateBean.class,
								"satisfiedState='" + 1 + "'" + " and creater='" + NovaApplication.getJgrno() + "'")
						.size() + "");
		tv_bmy.setText(
				NovaApplication.fdbmanger
						.findAllByWhere(JTYSDoctorEvaluateBean.class,
								"satisfiedState='" + 2 + "'" + " and creater='" + NovaApplication.getJgrno() + "'")
						.size() + "");
		tv_ybmy.setText(
				NovaApplication.fdbmanger
						.findAllByWhere(JTYSDoctorEvaluateBean.class,
								"satisfiedState='" + 4 + "'" + " and creater='" + NovaApplication.getJgrno() + "'")
						.size() + "");
		tv_fcbmy.setText(
				NovaApplication.fdbmanger
						.findAllByWhere(JTYSDoctorEvaluateBean.class,
								"satisfiedState='" + 5 + "'" + " and creater='" + NovaApplication.getJgrno() + "'")
						.size() + "");
		float manyi = NovaApplication.fdbmanger.findAllByWhere(JTYSDoctorEvaluateBean.class,
				"satisfiedState='" + 0 + "'" + " and creater='" + NovaApplication.getJgrno() + "'").size()
				+ NovaApplication.fdbmanger
						.findAllByWhere(JTYSDoctorEvaluateBean.class,
								"satisfiedState='" + 1 + "'" + " and creater='" + NovaApplication.getJgrno() + "'")
						.size();
		float all = NovaApplication.fdbmanger
				.findAllByWhere(JTYSDoctorEvaluateBean.class, " creater='" + NovaApplication.getJgrno() + "'").size();
		int myl = (int) ((manyi / all) * 100);
		tv_myl.setText(myl + "%");
		setProgressViewData(100,myl);

		// 2：儿童，3：孕产妇，4：老年人，5：重精神病，6：高血压，7：糖尿病，8：冠心病，9：脑卒中 10 残疾人默认值
	/*	int ertong = dbManager.getCountByJDRType(str_jdrno, PeopleType.CHILD);
		int yunchanfu = dbManager.getCountByJDRType(str_jdrno, PeopleType.WOMAN);
		int lnr = dbManager.getCountByJDRType(str_jdrno, PeopleType.OLD);
		int gxy = dbManager.getCountByJDRType(str_jdrno, PeopleType.GXY);
		int tnb = dbManager.getCountByJDRType(str_jdrno, PeopleType.TNB);
		int zdjsjb = dbManager.getCountByJDRType(str_jdrno, PeopleType.JSB);*/

		tv_ybrq.setText(String.valueOf(dbManager.getCountByJDRType(str_jdrno, PeopleType.YBRQ)));
		tv_gxy.setText(String.valueOf(dbManager.getCountByJDRType(str_jdrno, PeopleType.GXY)));
		tv_ertong.setText(String.valueOf(dbManager.getCountByJDRType(str_jdrno, PeopleType.CHILD)));
		tv_tnb.setText(String.valueOf(dbManager.getCountByJDRType(str_jdrno, PeopleType.TNB)));
		tv_ycf.setText(String.valueOf(dbManager.getCountByJDRType(str_jdrno, PeopleType.WOMAN)));
		tv_gxb.setText(String.valueOf(dbManager.getCountByJDRType(str_jdrno, PeopleType.GXB)));
		tv_lnr.setText(String.valueOf(dbManager.getCountByJDRType(str_jdrno, PeopleType.OLD)));
		tv_nzz.setText(String.valueOf(dbManager.getCountByJDRType(str_jdrno, PeopleType.NCZ)));
		tv_zxjsjb.setText(String.valueOf(dbManager.getCountByJDRType(str_jdrno, PeopleType.JSB)));
		tv_cjr.setText(String.valueOf(dbManager.getCountByJDRType(str_jdrno, PeopleType.CJR)));
		tv_fjh.setText(String.valueOf(dbManager.getCountByJDRType(str_jdrno, PeopleType.FJH)));

		/*
		 * List list = new ArrayList<>(); list.add(ertong); list.add(yunchanfu);
		 * list.add(lnr); list.add(gxy); list.add(tnb); list.add(zdjsjb);
		 * JTYSHomeAdapter adapter = new JTYSHomeAdapter(getContext(), list);
		 * lv_tixing.setAdapter(adapter);
		 */

	}

}
